package com.weme.mine.dialog

import android.content.Context
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.FragmentActivity
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.weme.common.databinding.DialogEdAgeBinding
import com.weme.common.view.pickerview.adapter.ArrayWheelAdapter
import com.weme.common.view.pickerview.contrarywind.adapter.WheelAdapter


interface EdAgeDialogListener {
    fun onAgeEntered(name: Int)
}

class EdAgeDialog(var mContext: FragmentActivity, var content: String) : BottomSheetDialog(mContext, com.weme.common.R.style.TUIBeautyDialogTheme) {
    private var listener: EdAgeDialogListener? = null
    private lateinit var binding: DialogEdAgeBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ImmersionBar.with(mContext).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).init()
        binding = DialogEdAgeBinding.inflate(LayoutInflater.from(context), null, false)
        setContentView(binding.root)
        window?.setWindowAnimations(com.weme.common.R.style.SlideUpAnimation)
        //setCancelable(false)
        bottomSheetBehavior = BottomSheetBehavior.from(binding.root.parent as View)
        bottomSheetBehavior!!.setBottomSheetCallback(mBottomSheetCallback)
        val adapter: WheelAdapter<String> = ArrayWheelAdapter(getData()) as WheelAdapter<String>

        binding.wheelView.adapter = adapter

        // 设置初始选中项
        binding.wheelView.currentItem = 0
        binding.wheelView.setLineSpacingMultiplier(2.5F)

        // 设置滚轮滚动监听器（可选）
        binding.wheelView.setOnItemSelectedListener { index -> // 处理选中项变化事件
         //   val selectedValue = adapter.getItem(index)

            binding.smvBtn.alpha = if(getData()[index] == content) 0.2f else 1F
        }
        binding.smvBtn.setOnClickListener {
            if (getData().get(binding.wheelView.currentItem).equals(content)||binding.smvBtn.alpha != 1F) {
                return@setOnClickListener
            }
            listener?.onAgeEntered(getData().get(binding.wheelView.currentItem).toInt())
            dismiss()
        }
        if (!TextUtils.isEmpty(content)) {
            binding.wheelView.currentItem = content.toInt() - 18
        }
    }

    private fun getData(): List<String> {
        val data: MutableList<String> = ArrayList()
        for (index in 18..99) {
            data.add(index.toString())
        } // 添加更多选项...
        return data
    }

    var bottomSheetBehavior: BottomSheetBehavior<*>? = null



    //此部分为禁止该弹窗可滑动
    private val mBottomSheetCallback: BottomSheetCallback = object : BottomSheetCallback() {
        override fun onStateChanged(bottomSheet: View, @BottomSheetBehavior.State newState: Int) {
            if (newState == BottomSheetBehavior.STATE_DRAGGING) { //判断为向下拖动行为时，则强制设定状态为展开
                bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            }
            if (newState == BottomSheetBehavior.STATE_SETTLING) {
                bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            }
            if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            }
            if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            }
            if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                bottomSheetBehavior!!.state = BottomSheetBehavior.STATE_EXPANDED
            }
        }

        override fun onSlide(bottomSheet: View, slideOffset: Float) { //  LogUtil.e(TAG, "onSlide——>" + slideOffset);
        }
    }

    fun setEdAgeDialogListener(listener: EdAgeDialogListener) {
        this.listener = listener
    }
}